package main 

import (
"path"
"flag"
"fmt"
"os"
"io/ioutil"
"strings"
)

var (
	del_txt *bool
)

type TxtVisitor struct{}

func (v *TxtVisitor) VisitDir(path string, f *os.FileInfo) bool {
	return true
}

func (v *TxtVisitor) VisitFile(p string, f *os.FileInfo) {
	if path.Ext(p) == ".txt"  {
		cont,err := ioutil.ReadFile(p)
		if err != nil {
			fmt.Printf("read err %v\n",err)
		}
		str := string(cont)
		str = strings.Replace(str,"\n","<p>",-1)
		p_new := append([]byte(p)[0:len(p)-4],[]byte(".html")...)
		str = "<html><p>" + str + "<p></html>"
		ioutil.WriteFile(string(p_new),[]byte(str),0644)
		if *del_txt{
			os.RemoveAll(p)
		}
	}
	return
}


func main(){
	del_txt = flag.Bool("b", false, "where del txt files")
	flag.Parse()
	v := &TxtVisitor{}
	path.Walk(".", v, nil)
}

